<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- 
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page session="true" %>
<%@ page errorPage="error.jsp" %> 
<%@ page import="java.security.KeyPair" %>
<%@ page import="info.niwota.search.CryptUtils" %>
<%@ page import="info.niwota.search.SearchEngine" %>
<%@ page import="info.niwota.search.Constants" %>
-->
<html>
<head>
<!--  
<%
	SearchEngine[] engines = (SearchEngine[]) session.getAttribute("my.engines");
	if (engines == null) {
		engines = SearchEngine.getEngines();
		session.setAttribute("my.engines", engines);
	}
	
	String service = (String) session.getAttribute(Constants.LAST_SERVICE);
	if (service == null) {
		service = "0";
	}
	String cook = (String) session.getAttribute(Constants.LAST_COOK);
	if (cook == null) {
		cook = "1";
	}
%>
-->
<title>Anonymous</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />

<link rel="shortcut icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="keyboard.css" />
<style type="text/css">
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="crypt.js"></script>
<script type="text/javascript" src="keyboard.js"></script>
<script type="text/javascript">
var spinner = '<img src="/spin.gif" alt="Please wait..." ><span style="color: white; font-size: xx-large;">...<blink>.....</blink></span>';

$.ajaxSetup ({  
	cache: false  
}); 

var rsaModulus = null;
var rsaExponent = null;

$(document).ready(function(){
	initForm();
});

function initForm() {
	$('#searchForm').submit(function() {
		var form = $(this);
		var kwf = $("#keyword", form);
		var dataf = $("#data", form);
		var kw = kwf.val() || '';
		
		dataf.val(rsaEncrypt(kw));
		kwf.val('');
			
		return true;
	});
	
	$("#search").click(function(e) {
		doSubmit();
     	return false;
	});
	
	$('#keyword').focus(function() {
		$('#status').text('');
	});
}

function doSubmit() {				
	var url = '/jsonp/crypto?type=json';
	var busy = showBusy(5000, 'Timeout, please try again.');

	$.getJSON(url, function(d) { 
		busy.cancel();
		if (d) {
			rsaModulus = d.modulus;
			rsaExponent = d.exponent;
			$('#searchForm').submit();
		}
	});
		
	return false;
}

function doSearch(s) {
	var sef = $("#service");
	sef.val(s);
	doSubmit();
}

function rsaEncrypt(s) {
	var rsa = new RSAKey();
	rsa.setPublic(rsaModulus, rsaExponent);
	var enc = rsa.encrypt(s);
	return linebrk(hex2b64(enc), 64);
}

function showError(msg) {
	$('#status').html(msg);
}

function showBusy(timeout, msg) {
	var tag = $('#busy');
	
	var t = timeout || 3000;
	var m = msg || '';
	tag.html(spinner);
	var timer = setTimeout(function(){ tag.html(m).css({ "color": "#ff0000" }); }, t);
	
	this.cancel = function() {
		clearTimeout(timer);
		tag.empty();
	}
	
	return this;
}

function toggle(s) {
	var el = window.document.getElementById(s);
	if(el.style.display == "block") {
		el.style.display = "none";
  	} else {
		el.style.display = "block";
	}
}
</script>
</head>

<body>
	<div id="topnav">
		<span style="color: #ffffff;">NIWOTA.INFO</span>
		<span style="color: #99FF66; margin-left: 16px;">Search the web anonymously</span> 
		<span class="top-right" style="margin-right: 8px;" ><span id="busy"></span></span>
	</div>

	<div id="page-content">

		<div style="margin-top: 16px;">
			<span id="status" style="color: #f00;"></span>
		</div>

		<div style="text-align: center;">
			<p style="font-size: x-large;">Anonymous <span><a href="#" onclick="toggle('options'); return false;" style="text-decoration: none;">?</a></span></p>
				
			<form method="post" id="searchForm" name="searchForm" autocomplete="off"
				action="/search" enctype="application/x-www-form-urlencoded">
				<div style="width: 380px; margin-left: auto; margin-right: auto; display:none;" id="options" >
					<ul style="text-align: left;">
					<li>Virtual keyboard against key logging</li>
					<li>Remove search term in result page<br />
					<input type="radio" name="cook" value="0" <%=(cook.equals("0") ? "checked=\"checked\"": "") %> /><span title="No change">No</span><br />
					<input type="radio" name="cook" value="1" <%=(cook.equals("1") ? "checked=\"checked\"": "") %> /><span title="Chop search term into small parts and mix them with random text">Obfuscate</span> <br />
					<input type="radio" name="cook" value="2" <%=(cook.equals("2") ? "checked=\"checked\"": "") %> /><span title="Replace search term with XXX">Cleanse</span><br />
					</li>
					<li>Encrypt all links (anchor, image...)<br />
					<input type="checkbox" name="encrypt" value="encrypt" checked="checked" disabled="disabled" /> 
					</li>
					</ul>
					<p style="text-align: left;">
					Pages are modified and may look different, especially form or any part depending on javascript may not work as designed. 
					A link in red is provided to access the original page but without the protection of anonymity.</p>
				</div>
				<input type="text" maxlength="2048" name="keyword" id="keyword" autocomplete="off" size="48" value="" class="keyboardInput" />
				<br />
				<input type="submit" value="<%= engines[Integer.parseInt(service)].name %>" id="search" name="search" />

				<input name="service" id="service" value="<%=service %>" type="hidden" />
				<input name="data" id="data" value="" type="hidden" />
			</form>
		</div>
		<div style="width: 380px; margin-left: auto; margin-right: auto;">
				<ul id="services" style="text-align: left; list-style-type: none;">
				<% for (int i = 0; i < engines.length; i++) { %>
				<li><a href="#" onclick="doSearch('<%=i %>'); return false;">
				<%=engines[i].name %></a></li> 
				<% } %>
				</ul>
		</div>
	</div>

	<div id="footer">Anonymous 1.0.0 &copy; 2011 All rights reserved</div>
</body>
</html>